'use strict';

/* Controllers */
  // signin controller
app.controller('regularListController', ['$scope', '$http', '$state', '$modal', 'user_info', 'toaster', function($scope, $http, $state, $modal, user_info, toaster) {
    $scope.loading = true;
    $scope.currentPage = 1;
    $scope.list = null;
    $scope.totalItem = 0;
    $scope.getPage = function(){
        $scope.loading = true;
        $http.get('/app/api/user/regular/list/' + $scope.currentPage)
            .then(function(response) {
                $scope.loading = false;
                let result = response.data.data;
                $scope.list = result.list;
                $scope.totalItem = result.totalItem;
            }, function(x) {

            });
    };
    user_info.check($scope).then(function(){
        $scope.getPage();
    });

    $scope.newRegular = function(){
        var modalInstance = $modal.open({
            templateUrl: '/tpl/modal/regular.html',
            controller: 'regularListController.newRegular',
            size: null,
            resolve:{
                title:function(){return "新的正则过滤"},
                info:function(){return {}}
            }
        });
        modalInstance.result.then(function (ruleInfo) {
            if($scope.list){
                toaster.pop("success", "新建正则成功");
                $scope.list.unshift(ruleInfo);
                if($scope.list.length > 10) $scope.list.pop();
            }
        }, function () {
            toaster.pop("error", "新建正则失败");
        });
    };
    $scope.modifyRegular = function(i, info){
        var modalInstance = $modal.open({
            templateUrl: '/tpl/modal/regular.html',
            controller: 'regularListController.newRegular',
            size: null,
            resolve:{
                title:function(){return "修改正则过滤"},
                info:function(){return info}
            }
        });
        modalInstance.result.then(function (ruleInfo) {
            toaster.pop("success", "修改正则成功");
            if($scope.list){
                $scope.list[i] = ruleInfo;
            }
        }, function () {
            toaster.pop("error", "修改正则失败");
        });
    };
    $scope.removeRegular = function(id){
        $http.get('/app/api/user/regular/del/' + id)
            .then(function(response) {
                toaster.pop("success", "删除正则成功");
                $scope.getPage();
            }, function(x) {
                toaster.pop("error", "删除正则失败", x.data.message);
            });
    };
}]);

app.controller('regularListController.newRegular', ['$scope', '$modalInstance', '$http', 'title', 'info', function($scope, $modalInstance, $http, title, info) {
    $scope.authError = null;
    $scope.title = title;
    $scope.info = info;
    $scope.testData = null;
    $scope.testResult = [];
    $scope.isRegular = false;
    $scope.buttonText = (title === "新的正则过滤" ? "添加" : "更改");
    var url = (title === "新的正则过滤" ? '/app/api/user/regular/new' : '/app/api/user/regular/modify/' + info._id);

    var updateTest = function(){
        try{
            if($scope.info.rule){
                var rule = new RegExp($scope.info.rule);
                if($scope.testData) $scope.testResult = $scope.testData.match(rule);
                $scope.isRegular = true;
            }
        }catch (e) {
            $scope.isRegular = false;
            console.error(e);
        }
    };
    $scope.$watch(function(){return $scope.info.rule}, updateTest);
    $scope.$watch("testData", updateTest);
    $scope.ok = function () {
        if($scope.isRegular){
            $http.post(url, $scope.info)
                .then(function(response) {
                    $modalInstance.close(response.data.data);
                }, function(x) {
                    $scope.authError = x.data.message;
                });
        } else {
            $scope.authError = "正则格式不正确或者还未填写"
        }
    };
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}]);